package cn.usian.basicInformation.sensitivewords.service.impl;

import cn.usian.basicInformation.sensitivewords.mapper.SensitiveWordsMapper;
import cn.usian.basicInformation.sensitivewords.service.ISensitiveWordsService;
import cn.usian.basicInformation.sensitivewords.config.SensitiveWordUtils;
import cn.usian.dto.basicInformation.sensitivewords.BaseSensitiveWordsDto;
import cn.usian.pojo.Result;
import cn.usian.pojo.basicInformation.sensitivewords.BaseSensitiveWords;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class SensitiveWordsServiceImpl implements ISensitiveWordsService {
    @Autowired
    private SensitiveWordsMapper swMapper;

    @Override
    public PageInfo<BaseSensitiveWords> list(BaseSensitiveWordsDto sensitiveWords) {
        PageHelper.startPage(sensitiveWords.getPageNum(), sensitiveWords.getPageSize());
        List<BaseSensitiveWords> list = swMapper.list(sensitiveWords);
        PageInfo<BaseSensitiveWords> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Result save(BaseSensitiveWords sensitiveWords) {
        //log.info("保存敏感词: {}", sensitiveWords);
        // 参数校验
        if (sensitiveWords == null || sensitiveWords.getSensitiveWords() == null) {
            return Result.error("敏感词不能为空");
        }
        try {
            // 首先查找是否已存在该敏感词
            BaseSensitiveWords words = swMapper.findBySensitiveWords(sensitiveWords.getSensitiveWords());
            if (words != null) {
                return Result.error("敏感词已存在");
            }
            if (sensitiveWords.getId() != null) {
                // 更新操作
                swMapper.update(sensitiveWords);
            } else {
                // 添加新记录
                swMapper.add(sensitiveWords);
            }
        } catch (Exception e) {
            // 记录异常信息
            log.error("保存敏感词时发生异常", e);
            return Result.error("保存失败: " + e.getMessage());
        }
        return Result.ok();
    }

    @Override
    public BaseSensitiveWords findById(long id) {
        return swMapper.findById(id);
    }

    @Override
    public Set<String> sensitiveWordFiltering(String text) {
        // 得到敏感词有哪些，传入2表示获取所有敏感词
        Set<String> sensitiveWord = SensitiveWordUtils.getSensitiveWord(text, 2);
        log.info("敏感词: {}", sensitiveWord);
        return sensitiveWord;
    }

    @Override
    public Result insert(BaseSensitiveWords sensitiveWords) {
        if (sensitiveWords == null || sensitiveWords.getSensitiveWords() == null) {
            return Result.error("敏感词不能为空");
        }
        try {
            // 首先查找是否已存在该敏感词
            BaseSensitiveWords words = swMapper.findBySensitiveWords(sensitiveWords.getSensitiveWords());
            if (words != null) {
                return Result.error("敏感词已存在");
            }
            // 添加新记录
            swMapper.add(sensitiveWords);
        } catch (Exception e) {
            // 记录异常信息
            log.error("保存敏感词时发生异常", e);
            return Result.error("保存失败: " + e.getMessage());
        }
        return Result.ok(sensitiveWords);
    }

    @Override
    public Result update(BaseSensitiveWords sensitiveWords) {
        // 参数校验
        if (sensitiveWords == null || sensitiveWords.getSensitiveWords() == null) {
            return Result.error("敏感词不能为空");
        }
        try {
            // 首先查找是否已存在该敏感词
            BaseSensitiveWords words = swMapper.findBySensitiveWords(sensitiveWords.getSensitiveWords());
            if (words != null) {
                return Result.error("敏感词已存在");
            }
            // 更新操作
            swMapper.update(sensitiveWords);
        } catch (Exception e) {
            // 记录异常信息
            log.error("保存敏感词时发生异常", e);
            return Result.error("保存失败: " + e.getMessage());
        }
        return Result.ok(sensitiveWords);
    }
}
